package DAL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.RSAKeys;
import entities.Voting;

public class RSAKeysDA {
	
	public RSAKeys[] selectRSAKeys(RSAKeys rsa)
	{
		boolean whereClause=false;
		String query = "SELECT * from RSAKeys  " ;
		if(rsa.getID()!=-1)
		{
			query += "WHERE ID = " + rsa.getID();
			whereClause = true;
		}
		if(rsa.getPublicKey()!="")
		{
			if(whereClause)
				query+="AND publicKey = '" + rsa.getPublicKey()+"'";
			else	
			{
				query += "WHERE publicKey = '" + rsa.getPublicKey()+"'";
				whereClause = true;
			}
		}
		if(rsa.getPrivateKey()!="")
		{
			if(whereClause)
				query+="AND privateKey = '" + rsa.getPrivateKey()+"'";
			 else	
			{	query += "WHERE privateKey = '" + rsa.getPrivateKey()+"'";
				whereClause = true;
			}
		}
		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		ArrayList<RSAKeys> rsas = new ArrayList<RSAKeys>();
		try
		{
			while(rs.next())
			{
				RSAKeys rsaTemp=new RSAKeys();
				rsaTemp.setID(rs.getInt("ID"));
				rsaTemp.setPublicKey(rs.getString("publicKey"));
				rsaTemp.setPrivateKey(rs.getString("privateKey"));
				rsas.add(rsaTemp);
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		RSAKeys[] keys=new RSAKeys[0];
		return rsas.toArray(keys);
	}
	public boolean  insertRSAKeys(RSAKeys key)
	{
		String query="INSERT INTO RSAKeys (publicKey , privateKey) VALUES ('"+key.getPublicKey()+"' , '"+key.getPrivateKey()+"')";
		ConnectionManager con = new ConnectionManager();
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	public boolean  updateRSAKeys(RSAKeys rsa)
	{
		boolean setClause=false;
		String query = "UPDATE RSAKeys SET " ;
		
		if(rsa.getPublicKey()!="")
		{
			if(setClause)
				query+=", publicKey = '" + rsa.getPublicKey()+"'";
			else	
			{
				query += " publicKey = '" + rsa.getPublicKey()+"'";
				setClause = true;
			}
		}
		if(rsa.getPrivateKey()!="")
		{
			if(setClause)
				query+=", privateKey = '" + rsa.getPrivateKey()+"'";
			 else	
			{	query += " privateKey = '" + rsa.getPrivateKey()+"'";
				setClause = true;
			}
		}
		
			query += " WHERE ID = " + rsa.getID();
			ConnectionManager con = new ConnectionManager();
			
			int x = con.executeUpdateQuery(query);
			
			return (x==1);
		
	}
	public int deleteRSAKeys(RSAKeys rsa)
	{
		boolean whereClause=false;
		String query = "DELETE FROM  RSAKeys  " ;
		if(rsa.getID()!=-1)
		{
			query += "WHERE ID = " + rsa.getID();
			whereClause = true;
		}
		if(rsa.getPublicKey()!="")
		{
			if(whereClause)
				query+="AND publicKey = '" + rsa.getPublicKey()+"'";
			else	
			{
				query += "WHERE publicKey = '" + rsa.getPublicKey()+"'";
				whereClause = true;
			}
		}
		if(rsa.getPrivateKey()!="")
		{
			if(whereClause)
				query+="AND privateKey = '" + rsa.getPrivateKey()+"'";
			 else	
			{	query += "WHERE privateKey = '" + rsa.getPrivateKey()+"'";
				whereClause = true;
			}
		}
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}

}
